package com.study.controller;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author MI
 * @ClassName SelectController.java
 * @createTime 2021年09月01日 12:16:00
 */
@Api(tags = "Hello-测试")
@Slf4j
@Controller
@RequestMapping("select")
public class SelectController {

    @GetMapping("")
    public String hello(Model model) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource("static/json/cityData.min.json");
        File file = classPathResource.getFile();
        String s = FileUtils.readFileToString(file);
        model.addAttribute("msg", s);
        return "select";
    }

    @GetMapping("sele")
    public String sele(Model model) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource("static/json/cityData.min.json");
        File file = classPathResource.getFile();
        String s = FileUtils.readFileToString(file);
        model.addAttribute("msg", s);
        return "sele";
    }

    @GetMapping("seleurl")
    public String seleurl(Model model) throws Exception {
        ClassPathResource classPathResource = new ClassPathResource("static/json/cityData.min.json");
        File file = classPathResource.getFile();
        String s = FileUtils.readFileToString(file);
        model.addAttribute("msg", s);
        return "seleurl";
    }


    @GetMapping("province")
    @ResponseBody
    public ArrayList<Pro> province() {
        ArrayList<Pro> pros = new ArrayList<>();
        pros.add(new Pro().setValue(1).setName("北京市"));
        pros.add(new Pro().setValue(2).setName("上海市"));
        pros.add(new Pro().setValue(3).setName("浙江省"));
        return pros;
    }

    @GetMapping("city")
    @ResponseBody
    public String city() {
        return "{\"state\":\"success\",\"data\":[{\"value\":301,\"name\":\"杭州市\"},{\"value\":302,\"name\":\"宁波市\"}]}";
    }

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    private static class Pro {
        private int value;
        private String name;
    }

    /**
     * 自定义选项|http://code.ciaoca.com/jquery/cxSelect/demo/custom
     *
     * @return
     */
    @GetMapping("selecustom")
    public String custom() {
        return "selecustom";
    }

    @GetMapping("customData")
    @ResponseBody
    public List<CustomSelVo> customData() {
        ArrayList<CustomSelVo> vos = new ArrayList<>();
        CustomSelVo vo1 = new CustomSelVo().setN("北京").setV("1");
        CustomSelVo vo1s1 = new CustomSelVo().setV("11").setN("西城区");
        CustomSelVo vo1s2 = new CustomSelVo().setV("12").setN("东城区");
        vo1.getS().add(vo1s1);
        vo1.getS().add(vo1s2);


        CustomSelVo vo2 = new CustomSelVo().setV("2").setN("浙江");
        CustomSelVo vo2s1 = new CustomSelVo().setV("21").setN("杭州");
        CustomSelVo vo2s2 = new CustomSelVo().setV("22").setN("宁波");
        vo2.getS().add(vo2s1);
        vo2.getS().add(vo2s2);

        vos.add(vo1);
        vos.add(vo2);
        return vos;
    }

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    @Accessors(chain = true)
    private static class CustomSelVo {
        private String v;
        private String n;
        private List s = new LinkedList();

    }

    @GetMapping("selecus")
    public String selecus() {
        return "selecus";
    }
}
